package com.dlxj.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Bugtype;
import com.dlxj.entity.Circuitry;
import com.dlxj.entity.Debugtask;
import com.dlxj.entity.Model;
import com.dlxj.entity.Patroltask;
import com.dlxj.entity.Receiptdebug;
import com.dlxj.entity.Receiptpatrol;
import com.dlxj.entity.User;
import com.dlxj.entity.WaiTransact;
import com.dlxj.service.PoleService;
import com.dlxj.service.WaiTransactService;
import com.dlxj.util.ConstClass;
import com.dlxj.util.PageBean;

public class WaiTransactServiceImpl implements WaiTransactService {

	BaseDAO baseDAO;
	String url;
	List<WaiTransact> waiTransacts;
	PoleService poleService;
	User user = null;

	@Override
	public PageBean findWaiTransactList(int pageIndex, int pageSize) {
		waiTransacts = new ArrayList<WaiTransact>();
		user = ConstClass.getCurrentUser();

		findPatrolTask();
		findDebug();
		System.out.println("=======待办列表=======");
		List<WaiTransact> result = new ArrayList<WaiTransact>();
		for (int i = 0; i < waiTransacts.size(); i++) {
			for (int j = i + 1; j < waiTransacts.size(); j++) {
				Date d1 = waiTransacts.get(i).getTime();
				Date d2 = waiTransacts.get(j).getTime();
				if (d1 != null && d2 != null && d1.before(d2)) {
					WaiTransact w = waiTransacts.get(i);
					waiTransacts.set(i, waiTransacts.get(j));
					waiTransacts.set(j, w);
				}
			}
		}
		PageBean pageBean = new PageBean();
		int total = waiTransacts.size();
		pageBean.setTotal(total);
		pageBean.setPageSize(pageSize);
		pageBean.setIndex(pageIndex);
		int pageCount = total / pageSize;
		pageCount += (total % pageSize != 0 ? 1 : 0);
		List rows = new ArrayList();
		for (int i = (pageIndex - 1) * pageSize; i < (pageIndex - 1) * pageSize
				+ pageSize; i++) {
			if (i >= total) {
				break;
			}
			rows.add(waiTransacts.get(i));
		}
		pageBean.setPageCount(pageCount);

		for (WaiTransact wt : waiTransacts) {
			System.out.println(wt.getType() + "\t" + wt.getName() + "\t"
					+ wt.getRouteNo() + "(" + wt.getStartNo() + "-"
					+ wt.getEndNo() + ")" + wt.getCreateName() + "\t"
					+ wt.getTime() + "\t" + wt.getUrl());
		}
		pageBean.setRows(rows);
		return pageBean;
	}

	private boolean isHavePurview(String modelName) {
		String mids = ConstClass.getCurrentUser().getGroup().getPurview()
				.getMid();
		String[] arr = mids.split(",");
		for (String mid : arr) {
			Model model = (Model) baseDAO.findById(Model.class,
					Integer.valueOf(mid));
			if (model.getName().equals(modelName)) {
				url = model.getUrl();
				return true;
			}
		}
		return false;
	}

	private String findUserNameById(int id) {
		User user = (User) baseDAO.findById(User.class, id);
		if (user != null) {
			return user.getName();
		}
		return null;
	}

	private String findBugNameById(int id) {
		Bugtype bugtype = (Bugtype) baseDAO.findById(Bugtype.class, id);
		if (bugtype != null) {
			return bugtype.getName();
		}
		return null;
	}

	private String findDeBugTaskNameById(String id) {
		Debugtask deBugtask = (Debugtask) baseDAO.findById(Debugtask.class, id);
		if (deBugtask != null) {
			return deBugtask.getName();
		}
		return null;
	}

	private void findDebug() {
		String[] level = { "一般", "紧急", "严重" };
		if (this.isHavePurview("消缺任务制定")) {
			List<Receiptpatrol> receiptpatrols = poleService.findHasBug();
			for (Receiptpatrol receiptpatrol : receiptpatrols) {
				String pid = receiptpatrol.getPole().getId();
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(receiptpatrol.getUid()));
				wt.setTime(receiptpatrol.getTime());
				wt.setName(pid + "待处理");
				wt.setType("消缺任务制定");
				wt.setRemark("缺陷类型:"
						+ this.findBugNameById(receiptpatrol.getBid()) + "  "
						+ "缺陷级别:" + level[receiptpatrol.getLevel() - 1]);
				wt.setUrl(url);
				waiTransacts.add(wt);
			}

		}
		if (this.isHavePurview("消缺任务分配")) {
			List<Debugtask> debugtasks = baseDAO.findByHql(
					"from Debugtask where state =? ",
					new Object[] { ConstClass.TASK_STATE_WAIT });
			for (Debugtask debugtask : debugtasks) {
				String pid = debugtask.getId();
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(debugtask.getCreateUser()));
				wt.setTime(debugtask.getTime());
				wt.setName(debugtask.getName() + "待处理");
				wt.setType("消缺任务分配");
				wt.setRemark(debugtask.getDescription());
				wt.setUrl(url);
				waiTransacts.add(wt);
			}

		}
		if (this.isHavePurview("消缺任务执行")) {
			List<Debugtask> debugtasks = baseDAO
					.findByHql(
							"from Debugtask where state =?  and id in (select did from DebugtaskUser where uid = ?)",
							new Object[] { ConstClass.TASK_STATE_ASSIGNED,
									user.getId() });
			for (Debugtask debugtask : debugtasks) {
				String pid = debugtask.getId();
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(debugtask.getCreateUser()));
				wt.setTime(debugtask.getTime());
				wt.setName(debugtask.getName() + "待执行");
				wt.setType("消缺任务执行");
				wt.setRemark(debugtask.getDescription());
				wt.setUrl(url);
				waiTransacts.add(wt);
			}

		}
		if (this.isHavePurview("消缺任务回执")) {
			List<Debugtask> debugtasks = baseDAO
					.findByHql(
							"from Debugtask where state =? and id in (select did from DebugtaskUser where uid = ?)",
							new Object[] { ConstClass.TASK_STATE_EXECUTING,
									user.getId() });
			for (Debugtask debugtask : debugtasks) {
				String pid = debugtask.getId();
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(debugtask.getCreateUser()));
				wt.setTime(debugtask.getTime());
				wt.setName(debugtask.getName() + "待回执");
				wt.setType("消缺任务回执");
				wt.setRemark(debugtask.getDescription());
				wt.setUrl(url);
				waiTransacts.add(wt);
			}

		}
		if (this.isHavePurview("消缺任务审批")) {
			List<Receiptdebug> receiptdebugs = baseDAO.findByHql(
					"from Receiptdebug where state =? ",
					new Object[] { ConstClass.TASK_AUDITING_MISSPASS });
			for (Receiptdebug receiptdebug : receiptdebugs) {
				String pid = receiptdebug.getDid();
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(receiptdebug.getUid()));
				wt.setTime(receiptdebug.getTime());
				wt.setName(findDeBugTaskNameById(pid) + "待审批");
				wt.setType("消缺任务审批");
				wt.setRemark(receiptdebug.getDescription());
				wt.setUrl(url);
				waiTransacts.add(wt);
			}

		}

	}

	private void findPatrolTask() {

		if (this.isHavePurview("巡检任务分配")) {
			List<Patroltask> patroltasks = baseDAO.findByHql(
					"from Patroltask where state =? ",
					new Object[] { ConstClass.TASK_STATE_WAIT });
			for (Patroltask patroltask : patroltasks) {
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(patroltask.getUid()));
				wt.setStartNo(patroltask.getStartPoleNo());
				wt.setEndNo(patroltask.getEndPoleNo());
				wt.setTime(patroltask.getTime());
				wt.setName(patroltask.getName() + "待分配巡检员");
				wt.setType("巡检任务分配");
				wt.setRouteNo(patroltask.getCid());
				wt.setUrl(url);
				waiTransacts.add(wt);
			}
			url = null;
		}
		if (this.isHavePurview("巡检任务执行")) {
			List<Patroltask> patroltasks = baseDAO
					.findByHql(
							"from Patroltask where state =? and id in (select pid from PatroltaskUser where uid = ?)",
							new Object[] { ConstClass.TASK_STATE_ASSIGNED,
									user.getId() });
			for (Patroltask patroltask : patroltasks) {
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(patroltask.getUid()));
				wt.setStartNo(patroltask.getStartPoleNo());
				wt.setEndNo(patroltask.getEndPoleNo());
				wt.setTime(patroltask.getTime());
				wt.setName(patroltask.getName() + "待执行");
				wt.setRouteNo(patroltask.getCid());
				wt.setType("巡检任务执行");
				wt.setUrl(url);
				waiTransacts.add(wt);
			}
			url = null;
		}
		if (this.isHavePurview("巡检任务回执")) {
			List<Patroltask> patroltasks = baseDAO
					.findByHql(
							"from Patroltask where state =? and id in (select pid from PatroltaskUser where uid = ?)",
							new Object[] { ConstClass.TASK_STATE_EXECUTING,
									user.getId() });
			for (Patroltask patroltask : patroltasks) {
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(patroltask.getUid()));
				wt.setStartNo(patroltask.getStartPoleNo());
				wt.setEndNo(patroltask.getEndPoleNo());
				wt.setTime(patroltask.getTime());
				wt.setRouteNo(patroltask.getCid());
				wt.setName(patroltask.getName() + "待回执录入");
				wt.setType("巡检任务回执");
				wt.setUrl(url);
				waiTransacts.add(wt);
			}
			url = null;
		}
		if (this.isHavePurview("缺陷等级确认")) {
			List<Patroltask> patroltasks = baseDAO.findByHql(
					"from Patroltask where state =?",
					new Object[] { ConstClass.TASK_STATE_FINISH });
			for (Patroltask patroltask : patroltasks) {
				WaiTransact wt = new WaiTransact();
				wt.setCreateName(findUserNameById(patroltask.getUid()));
				wt.setStartNo(patroltask.getStartPoleNo());
				wt.setEndNo(patroltask.getEndPoleNo());
				wt.setTime(patroltask.getTime());
				wt.setName(patroltask.getName() + "待确认");
				wt.setType("缺陷等级确认");
				wt.setUrl(url);
				wt.setRouteNo(patroltask.getCid());
				waiTransacts.add(wt);
			}
			url = null;
		}

	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public PoleService getPoleService() {
		return poleService;
	}

	public void setPoleService(PoleService poleService) {
		this.poleService = poleService;
	}

}
